#include "FrameBufferHandler.h"

FrameBufferHandler::FrameBufferHandler(void)
{
}

FrameBufferHandler::~FrameBufferHandler(void)
{
}

void FrameBufferHandler::SetUpFBO( int bufferType, int width, int height, int texNum, GLenum interFormat, GLenum format, GLuint type, GLenum target/*=GL_TEXTURE_2D*/ )
{
	framebuffer.width=width;
	framebuffer.height=height;
	framebuffer.interFormat=interFormat;
	framebuffer.format=format;
	framebuffer.target=target;
	framebuffer.type=type;
	framebuffer.texNum=texNum;
	framebuffer.texID=(GLenum*)malloc(texNum*sizeof(GLenum));
	framebuffer.buffers=(GLenum*)malloc(texNum*sizeof(GLenum));
	this->bufferType=bufferType;

	for (int i=0;i<texNum;i++)
	{
		framebuffer.buffers[i]=GL_COLOR_ATTACHMENT0_EXT+i;
	}

	switch (bufferType)
	{
	case TEX:
		InitTexBuffer();
		break;

	case DEP:
		InitDepBuffer();
		break;
	}

}

void FrameBufferHandler::InitTexBuffer()
{
	glGenTextures(framebuffer.texNum,framebuffer.texID);

	for (int i=0;i<framebuffer.texNum;i++)
	{
		glBindTexture(framebuffer.target,framebuffer.texID[i]);
		glTexParameteri(framebuffer.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(framebuffer.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(framebuffer.target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(framebuffer.target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexImage2D(framebuffer.target, 0, framebuffer.interFormat, framebuffer.width, framebuffer.height, 0, framebuffer.format, framebuffer.type, NULL);
	}
}

void FrameBufferHandler::FBOActivate()
{
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,framebuffer.ID);

	switch (this->bufferType)
	{
	case TEX:
		for (int i=0;i<framebuffer.texNum;i++)
		{
			glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT+i,framebuffer.target,framebuffer.texID[i],NULL);	
		}
		glDrawBuffers(framebuffer.texNum,framebuffer.buffers);
		break;

	case DEP:
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, framebuffer.target, framebuffer.texID[0], NULL);
		break;
	}

}

void FrameBufferHandler::FBODeactivate()
{
	//glReadPixels(0, 0, framebuffer.width, framebuffer.height,GL_RGB,GL_FLOAT,buffer);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

void FrameBufferHandler::SetBufferID( GLuint ID )
{
	framebuffer.ID=ID;
}

void FrameBufferHandler::InitDepBuffer()
{

	glGenTextures(1,&(framebuffer.texID[0]));
	glBindTexture(framebuffer.target,framebuffer.texID[0]);
	glTexParameteri(framebuffer.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(framebuffer.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(framebuffer.target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(framebuffer.target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(framebuffer.target, 0, framebuffer.interFormat, framebuffer.width, framebuffer.height, 0, framebuffer.format, framebuffer.type, NULL);
	//glTexImage2D(framebuffer.target, 0, framebuffer.interFormat, framebuffer.width, framebuffer.height, 0, GL_DEPTH_COMPONENT, GL_INT, NULL);

}


